﻿
@{
    ViewBag.Title = "组件管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}
@model IOA.MES.DataAccess.VModel.VWorkOrder

@section Contents{
    <style>
        .layui-col-xs3 {
            padding: 10px 0;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card layui-form">
            <div class="layui-card-header">
                {{lang.Field.WorkorderInfo}}：
            </div>
            <div class="layui-card-body">
                <div class="layui-row">
                    <div class="layui-col-xs3 layui-inline">
                        <label class="layui-form-label">{{lang.Field.OrderNo}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" lay-skin="primary" class="layui-input" value="@Model.OrderNo" disabled>
                        </div>
                    </div>
                    <div class="layui-col-xs3 layui-inline">
                        <label class="layui-form-label">{{lang.Field.WorkorderNo}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" lay-skin="primary" class="layui-input" value="@Model.WorkOrderNo" disabled>
                        </div>
                    </div>
                    <div class="layui-col-xs3 layui-inline">
                        <label class="layui-form-label">{{lang.Field.ErpOrderNo}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" lay-skin="primary" class="layui-input" value="@Model.ErpWorkOrderNo" disabled>
                        </div>
                    </div>
                    <div class="layui-col-xs3 layui-inline">
                        <label class="layui-form-label">{{lang.Field.ErpOrderRowNo}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" lay-skin="primary" class="layui-input" value="@Model.ErpWorkOrderRowNum" disabled>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-xs3 layui-inline">
                        <label class="layui-form-label">{{lang.Field.WorkShop}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" lay-skin="primary" class="layui-input" value="@Model.WorkshopName" disabled>
                        </div>
                    </div>
                    <div class="layui-col-xs3 layui-inline">
                        <label class="layui-form-label">{{lang.Field.ApprovalTime}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" lay-skin="primary" class="layui-input" value="@(Model.ReviewTime?.ToString("yyyy-MM-dd HH:mm:ss"))" disabled>
                        </div>
                    </div>
                    <div class="layui-col-xs3 layui-inline">
                        <label class="layui-form-label">{{lang.Field.CommencementDate}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" lay-skin="primary" class="layui-input" value="@(Model.StartDate?.ToString("yyyy-MM-dd"))" disabled>
                        </div>
                    </div>
                    <div class="layui-col-xs3 layui-inline">
                        <label class="layui-form-label">{{lang.Field.CompletionDate}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" lay-skin="primary" class="layui-input" value="@(Model.EndDate?.ToString("yyyy-MM-dd"))" disabled>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-xs3 layui-inline">
                        <label class="layui-form-label">{{lang.Field.FinishedProductCode}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" lay-skin="primary" class="layui-input" value="@Model.ProductCode" disabled>
                        </div>
                    </div>
                    <div class="layui-col-xs3 layui-inline">
                        <label class="layui-form-label">{{lang.Field.FinishedProductName}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" lay-skin="primary" class="layui-input" value="@Model.ProductName" disabled>
                        </div>
                    </div>
                    <div class="layui-col-xs3 layui-inline">
                        <label class="layui-form-label">{{lang.Field.Count}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" lay-skin="primary" class="layui-input" value="@Model.Count" disabled>
                        </div>
                    </div>
                    <div class="layui-col-xs3 layui-inline">
                        <label class="layui-form-label">{{lang.Field.ProduceStatus}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" lay-skin="primary" class="layui-input" value="@Model.CNStatus" disabled>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card layui-form">
            <div class="layui-card-header">
                {{lang.Field.BarcodeGeneration}}：
                <input type="hidden" name="WorkOrderNo" value="@Model.WorkOrderNo" />
                <input type="hidden" name="OrderNo" value="@Model.OrderNo" />
            </div>
            <div class="layui-card-body">
                <div class="layui-tab">
                    <ul class="layui-tab-title">
                        <li class="layui-this">{{lang.Field.SingleBarcode}}</li>
                        <li>{{lang.Field.MultipleBarcode}}</li>
                        <li>{{lang.Field.BatchBarcode}}</li>
                    </ul>
                    <div class="layui-tab-content">
                        <div class="layui-tab-item layui-show">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <div class="layui-input-inline" style="width:400px;">
                                        <input type="text" name="SingleCode" class="layui-input" placeholder="请输入条码" />
                                    </div>
                                    <button type="button" lay-submit lay-filter="submit1" class="layui-btn layui-btn-normal">{{lang.Field.GenerateBarcode}}</button>
                                </div>
                            </div>
                        </div>
                        <div class="layui-tab-item">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <div class="layui-input-inline" style="margin-left: 0;width: 400px;">
                                        <textarea name="MultipleCode" class="layui-textarea" placeholder="请输入条码，可多行输入"></textarea>
                                    </div>
                                    <button type="button" lay-submit lay-filter="submit2" class="layui-btn layui-btn-normal">{{lang.Field.GenerateBarcode}}</button>
                                </div>
                            </div>
                        </div>
                        <div class="layui-tab-item">
                            <div class="layui-row">
                                <div class="layui-col-md4">
                                    <div class="layui-inline">
                                        <label class="layui-form-label" style="padding-left: 0;text-align: left;">{{lang.Field.BarcodeFront}}：</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="CodeFront" class="layui-input" placeholder="请输入条码前端" />
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md4">
                                    <div class="layui-inline">
                                        <label class="layui-form-label" style="padding-left: 0;text-align: left;">{{lang.Field.BarcodeEnd}}：</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="CodeEnd" class="layui-input" placeholder="请输入条码末尾" />
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md4">
                                    <div class="layui-inline">
                                        <label class="layui-form-label" style="padding-left: 0;text-align: left;">{{lang.Field.NumberOfDigits}}：</label>
                                        <div class="layui-input-inline">
                                            <input type="number" name="SerialNumCount" class="layui-input" placeholder="请输入序号位数" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-row" style="margin-top:20px;">
                                <div class="layui-col-md3">
                                    <div class="layui-inline">
                                        <label class="layui-form-label" style="padding-left: 0;text-align: left;">{{lang.Field.StartSequenceNumber}}：</label>
                                        <div class="layui-input-inline" style="width:150px;">
                                            <input type="number" name="StartNum" class="layui-input startendnum" placeholder="请输入起始序号" />
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md3">
                                    <div class="layui-inline">
                                        <label class="layui-form-label" style="padding-left: 0;text-align: left;">{{lang.Field.EndSequenceNumber}}：</label>
                                        <div class="layui-input-inline" style="width:150px;">
                                            <input type="number" name="EndNum" class="layui-input startendnum" placeholder="请输入截止序号" />
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md3">
                                    <div class="layui-inline">
                                        <label class="layui-form-label" style="padding-left: 0;text-align: left;">{{lang.Field.BarCodeQuantity}}：</label>
                                        <div class="layui-input-inline" style="width:150px;">
                                            <input type="number" name="CodeCount" class="layui-input" placeholder="0" disabled />
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md3">
                                    <div class="layui-inline">
                                        <button type="button" lay-submit lay-filter="submit3" class="layui-btn layui-btn-normal">{{lang.Field.GenerateBarcode}}</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer', 'form', 'element'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form;

                var options = {
                    elem: '#table'
                    , url: '/Plan/QueryComponents'
                    , method: "post"
                    , where: { workOrderNo: "@Model.WorkOrderNo" }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers" }
                        , { field: 'BarCode', title: '组件条码' }
                        , { field: 'CreateTimeStr', title: '生成时间' }
                        , {
                            title: '分片状态', templet: function (d) {
                                return d.Distributed ? "已分片" : "未分片";
                            }
                        }
                        , { field: 'DistributeTimeStr', title: '分片时间' }
                        , { field: 'CNProduceStatus', title: '生产状态' }
                    ]]
                };

                queryTable();
                function queryTable() {
                    options.page.curr = 1;
                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }

                $(document).on("keyup", ".startendnum", function () {
                    var startNum = $("input[name=StartNum]").val() == "" ? null : parseInt($("input[name=StartNum]").val().trim()),
                        endNum = $("input[name=EndNum]").val() == "" ? null : parseInt($("input[name=EndNum]").val().trim());
                    $("input[name=CodeCount]").val((startNum == null || endNum == "" || startNum > endNum) ? "" : endNum - startNum + 1);
                });

                form.on('submit(submit1)', function (data) {
                    var field = data.field;
                    if (field.SingleCode.trim() == '') {
                        layer.msg("请填写条码", { icon: 5 });
                        return;
                    }
                    data.field.DistributeType = 0;
                    submit(field);
                    return false;
                });
                form.on('submit(submit2)', function (data) {
                    var codes = data.field.MultipleCode.trim();
                    if (codes == '') {
                        layer.msg("请至少填写1个条码", { icon: 5 });
                        return;
                    }

                    data.field.DistributeType = 1;
                    submit(data.field, codes.split('\n'));
                    return false;
                });
                form.on('submit(submit3)', function (data) {
                    var field = data.field;
                    if (field.SerialNumCount.trim() == '') {
                        layer.msg("请填写序号位数", { icon: 5 });
                        return;
                    } else if (field.StartNum.trim() == '') {
                        layer.msg("请填写起始序号", { icon: 5 });
                        return;
                    } else if (field.EndNum.trim() == '') {
                        layer.msg("请填写截止序号", { icon: 5 });
                        return;
                    }

                    data.field.DistributeType = 2;
                    submit(data.field);
                    return false;
                });
                function submit(data, codes) {
                    console.log(data);
                    var loadId = layer.load();
                    $.ajax({
                        url: "/Plan/CreateComponents",
                        data: {
                            model: data,
                            barCodes: codes ? codes : ""
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            layer.msg("生成成功！");
                            queryTable();
                        }
                    });
                }
            });
        }
    </script>
}
